﻿using Assets.Scripts.Interface;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Collider))]
public class Gate :EventObj, Receiver<string>{
    [SerializeField]
    private bool isOpen = false;//是否开着门
    [SerializeField,Tooltip("开门的密码.若为空则无密码")]
    private string passWord="password";
    private Collider co;
    private Animator ani;
    private ObtainMusic om;
    [SerializeField, Tooltip("提示的文字")]
    private string info= "身份识别失败";
    //开关门属性
    public bool IsOpen
    {
        get
        {
            return isOpen;
        }

        set
        {
            
            if (value)
            {
                co.isTrigger = true;//开门不再碰撞,但是仍然检测是否离开门
                ani.SetBool("isOpen", true);//设置门的动画机
                if (om != null) om.ChangeClip(om.musicGather[0]);
            }
            else if(isOpen==true)
            {
                co.isTrigger= false;
                ani.SetBool("isOpen", false);//设置门的动画机
                if (om != null) om.ChangeClip(om.musicGather[0]);
            }
            isOpen = value;
        }
    }
    //是否需要密码
    public bool NeedWord
    {
        get {return passWord != "Null"; }
    }

    public void Reveive(TMessage<string> t)
    {
        //print(t.Message);
        if (t.Message==passWord||passWord=="")//收到的是密码就通过
        {
            IsOpen = true;
        }
        else
        {
            info = "密码错误,识别失败";
            Cue(info);
        }
    }

    private void OnTriggerExit(Collider other)//如果有东西从门离开，则关门
    {
        string tag = other.tag;
        if(tag==GameManager.PLAYER||tag==GameManager.ENTITY)
        IsOpen = false;
    }

    /// <summary>
    /// 处理碰撞事件
    /// </summary>
    /// <param name="collision"></param>
    private void OnCollisionEnter(Collision collision)
    {
        string tag = collision.collider.tag;
        //print(collision.collider.tag);
        if (tag == "Player")//如果是玩家
        {
            // print("rece");
            //AProp k = PropManager.Instance.Items[(int)PropType.FUNCTION];
            //if (!(k is Key) || k == null)//如果有钥匙切
            if (passWord == "")
            {
                IsOpen = true;
                return;
            }
            if(collision.transform.GetComponent<Player>().collisionEnter == null)
            {
                info = "you must have a key";
                Cue(info);
            }
        }
        if (tag==GameManager.ENTITY)//如果怪物触碰到无密码的门也会开门
        {
            if (passWord == "")
            {
                IsOpen = true;
                return;
            }
        }
        
    }


    private void Awake()
    {
        ani = GetComponentInChildren<Animator>();
        co = GetComponent<Collider>();//获取碰撞体控件
#if DEBUG
        if (ani == null) Debug.LogError("们没有找到动画");
        if (co==null)//判空
        {
            throw new PrefabException("Gate has't collider");
        }
#endif
        om = GetComponent<ObtainMusic>();
    }
    private void Cue(string str)
    {
       // print("show");
        if (transform.GetChild(0).GetChild(0).localScale == new Vector3(0, 0, 0))
            UIshower.Instance.ShowFromSize(transform.GetChild(0).GetChild(0), new Vector3(1, 1, 1), 0.1f, 2f, str);
    }
}
